package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.entity.BZFLEntity;
import com.example.yiwei.crm.entity.ManyiduEntity;
import com.example.yiwei.crm.json.YYSpinnerJson;
import com.example.yiwei.crm.json.coordinationjson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class AddMYDActivity extends AppCompatActivity {
    private TextView txt_manager,txt_name,txt_age,txt_sex,txt_phone,txt_ly_time,txt_ys,txt_qk,txt_yy,txt_jd,txt_time,txt_yb,add_myd;
    private EditText edit_mxb,edit_yy;
    private Spinner sp_ly_l,sp_ly_r,sp_jz_l,sp_jz_r,sp_pj,sp_bz;
    private Button button1,button2;
    private TimePickerView mpv;
    private TimePickerView mpv1;
    private List<BZFLEntity> list3;
    private Date date1,date2;
    private int bz_id,pj_id;
    private String bz_s;
    private SharedPreferences sp;
    private List<String> list_pj;
    private String lysj,sj,ssp_ly_l,ssp_yl_r,ssp_jz_l,ssp_jzy;
    private List<String> list,bz_list;
    private ManyiduEntity manyiduEntity;
    private List<ManyiduEntity> mlist;
    private ArrayAdapter adapter1,adapter2,adapter3,adapter4,adapter5,adapter6;
    private ImageView im_back,im_comment;
    String result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_myd);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        add_myd = (TextView) findViewById(R.id.add_myd);
        list = new ArrayList<>();
        list.add("");
        list.add("2.0(5.3)");
        list.add("1.5(5.2)");
        list.add("1.2(5.1)");
        list.add("1.0(5.0)");
        list.add("0.8(4.9)");
        list.add("0.6(4.8)");
        list.add("0.5(4.7)");
        list.add("0.4(4.6)");
        list.add("0.3(4.5)");
        list.add("0.25(4.4)");
        list.add("0.2(4.3)");
        list.add("0.15(4.2)");
        list.add("0.12(4.1)");
        list.add("0.1(4.0)");
        list.add("0.08");
        list.add("0.06");
        list.add("0.04");
        list.add("0.02");
        list.add("指数");
        list.add("手动");
        list.add("光感");
        list.add("无光感");
        list_pj = new ArrayList<>();
        list_pj.add("非常满意");
        list_pj.add("满意");
        list_pj.add("一般");
        list_pj.add("不满意");
        txt_manager = (TextView) findViewById(R.id.myd_add_manger_tv);
        im_back = (ImageView) findViewById(R.id.myd_add_img_back);
        im_comment = (ImageView) findViewById(R.id.myd_add_img_commit);
        txt_name = (TextView) findViewById(R.id.myd_add_name);
        txt_age = (TextView) findViewById(R.id.myd_add_age);
        txt_sex = (TextView) findViewById(R.id.myd_add_sex);
        txt_phone = (TextView) findViewById(R.id.myd_add_phone);
        txt_ly_time = (TextView) findViewById(R.id.myd_add_ly_time);
        txt_ys = (TextView) findViewById(R.id.myd_add_ys_tv);
        txt_qk = (TextView) findViewById(R.id.myd_add_jzqk_tv);
        txt_yy = (TextView) findViewById(R.id.myd_add_jzyy_tv);
        txt_jd = (TextView) findViewById(R.id.myd_add_jdr_tv);
        txt_time = (TextView) findViewById(R.id.myd_add_time);
        edit_yy = (EditText) findViewById(R.id.myd_add_yy_edit);
        txt_yb = (TextView) findViewById(R.id.myd_add_yb_number);
        edit_mxb = (EditText) findViewById(R.id.myd_add_mxb);
        sp_ly_l = (Spinner) findViewById(R.id.myd_ly_l_sp);
        sp_ly_r = (Spinner) findViewById(R.id.myd_ly_r_sp);
        sp_jz_r = (Spinner) findViewById(R.id.myd_jz_r_sp);
        sp_jz_l = (Spinner) findViewById(R.id.myd_jz_l_sp);
        sp_pj = (Spinner) findViewById(R.id.myd_add_pj_sp);
        sp_bz = (Spinner) findViewById(R.id.myd_add_bz);
        button1 = (Button) findViewById(R.id.myd_add_btn_show1);
        button2 = (Button) findViewById(R.id.myd_add_time_btn_show1);
        //创建选择器
        mpv = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv.setTime(new Date());
        //选择监听
        mpv.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date1 =date;
                lysj=getTime(date);
                txt_ly_time.setText(lysj);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv.show();
            }
        });
        //创建选择器
        mpv1 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar1 = Calendar.getInstance();
        mpv1.setRange(calendar.get(Calendar.YEAR) - 100, calendar1.get(Calendar.YEAR) + 100);
        //插入时间
        mpv1.setTime(new Date());
        //选择监听
        mpv1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date2 = date;
                sj=getTime(date);
                txt_time.setText(sj);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mpv1.show();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject object = new JSONObject();
                try {
                    object.put("KYA01",getIntent().getIntExtra("KYA01",0));
                    object.put("DEL01",0);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                if ("update".equals(getIntent().getStringExtra("tag"))){
                    manyiduEntity = coordinationjson.jsonmanyiduMessage(baseClass.find_one_myd_msg_URL,json);
                    Log.d("ccc","bbbb");
                }else {
                    manyiduEntity = coordinationjson.jsonmanyiduMessage(baseClass.find_myd_msg_URL,json);
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if ("update".equals(getIntent().getStringExtra("tag"))){
                            setMessage();
                        }
                        initData();
                    }
                });
            }
        }).start();
        adapter1 = new ArrayAdapter(AddMYDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list);
        adapter1.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_ly_l.setAdapter(adapter1);
        sp_ly_l.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position!=0){
                    ssp_ly_l= list.get(position);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        adapter2 = new ArrayAdapter(AddMYDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list);
        adapter2.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_ly_r.setAdapter(adapter2);
        sp_ly_r.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position!=0){
                    ssp_yl_r= list.get(position);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        adapter3 = new ArrayAdapter(AddMYDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list);
        adapter3.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_jz_l.setAdapter(adapter3);
        sp_jz_l.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position!=0){
                    ssp_jz_l= list.get(position);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        adapter4 = new ArrayAdapter(AddMYDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list);
        adapter4.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_jz_r.setAdapter(adapter4);
        sp_jz_r.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (position!=0){
                    ssp_jzy= list.get(position);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc1 = new SC();
                JSONObject object3 = sc1.storage(sp);
                String json2 = object3.toString();
                String result2 = NetWorkUtils.getCount(baseClass.find_BZFL_URL,json2);
                list3 = YYSpinnerJson.getBZFL(result2);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        bz_list= new ArrayList<>();
                        bz_list.add("");
                        for (int i2=0;i2<list3.size();i2++){
                            bz_list.add(list3.get(i2).getName());
                        }
                        adapter5 = new ArrayAdapter(AddMYDActivity.this,R.layout.spinner_display_style,R.id.txtvwSpinner,bz_list);
                        adapter5.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        sp_bz.setAdapter(adapter5);
                        sp_bz.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                if (position>0){
                                    bz_s=list3.get(position-1).getName();
                                    bz_id = list3.get(position-1).getId();
                                }
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                            for (int i=0;i<list3.size();i++){
                                if (manyiduEntity.getBz_id()==list3.get(i).getId()){
                                    sp_bz.setSelection(i+1);
                                }
                            }

                    }

                });
            }
        }).start();
        adapter6 = new ArrayAdapter(AddMYDActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, list_pj);
        adapter6.setDropDownViewResource(R.layout.spinner_dropdown_style);
        sp_pj.setAdapter(adapter6);
        sp_pj.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                pj_id= position+1;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        im_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        im_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (txt_time.getText().toString().equals("")){
                    Toast.makeText(AddMYDActivity.this, "请选择日期", Toast.LENGTH_SHORT).show();
                }else {
                    add();
                }

            }
        });
    }
    public void setMessage(){
        add_myd.setText("修改满意度");
        txt_time.setText(manyiduEntity.getDate());
        txt_ly_time.setText(manyiduEntity.getHospital_time());
        Log.d("aaaaa",manyiduEntity.getJz_left());
        if (manyiduEntity.getLy_left().equals(manyiduEntity.getAly_left())){
            for (int i= 0;i<list.size();i++){
                if (manyiduEntity.getLy_left().equals(list.get(i))){
                    sp_ly_l.setSelection(i);
                }
            }
        }else {
            for (int i= 0;i<list.size();i++){
                if ((manyiduEntity.getLy_left()+"("+manyiduEntity.getAly_left()+")").equals(list.get(i))){
                    sp_ly_l.setSelection(i);
                }
            }
        }
        if (manyiduEntity.getLy_right().equals(manyiduEntity.getAly_right())){
            for (int i= 0;i<list.size();i++){
                if (manyiduEntity.getLy_right().equals(list.get(i))){
                    sp_ly_r.setSelection(i);
                }
            }
        }else {
            for (int i= 0;i<list.size();i++){
                if ((manyiduEntity.getLy_right()+"("+manyiduEntity.getAly_right()+")").equals(list.get(i))){
                    sp_ly_r.setSelection(i);
                }
            }
        }
        if (manyiduEntity.getJz_left().equals(manyiduEntity.getAjz_left())){
            for (int i= 0;i<list.size();i++){
                if (manyiduEntity.getJz_left().equals(list.get(i))){
                    sp_jz_l.setSelection(i);
                }
            }
        }else {
            for (int i= 0;i<list.size();i++){
                if ((manyiduEntity.getJz_left()+"("+manyiduEntity.getAjz_left()+")").equals(list.get(i))){
                    sp_jz_l.setSelection(i);
                }
            }
        }
        if (manyiduEntity.getJz_right().equals(manyiduEntity.getAjz_right())){
            for (int i= 0;i<list.size();i++){
                if (manyiduEntity.getAjz_left().equals(list.get(i))){
                    sp_jz_r.setSelection(i);
                }
            }
        }else{
            for (int i=0;i<list.size();i++){
                if ((manyiduEntity.getJz_right()+"("+manyiduEntity.getAjz_right()+")").equals(list.get(i))){
                    sp_jz_r.setSelection(i);
                }
            }
        }

        if (manyiduEntity.getManyidu()!=0){
            sp_pj.setSelection(manyiduEntity.getManyidu()-1);
        }
        edit_mxb.setText(manyiduEntity.getMxb());
        edit_yy.setText(manyiduEntity.getResult());
    }
    public void initData(){
        txt_manager.setText(manyiduEntity.getManager_name());
        txt_name.setText(manyiduEntity.getName());
        txt_age.setText(manyiduEntity.getAge()+"");
        txt_sex.setText(manyiduEntity.getGender());
        txt_phone.setText(manyiduEntity.getPhone());
        txt_yb.setText(manyiduEntity.getYb());
        txt_ys.setText(manyiduEntity.getDoctor_name());
        txt_qk.setText(manyiduEntity.getState());
        txt_yy.setText(manyiduEntity.getHospital());
        txt_jd.setText(sp.getString("CID03",null));
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
    public void add(){

        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                try {
                    object.put("CID03",sp.getString("CID03",null));
                    object.put("KYA01",getIntent().getIntExtra("KYA01",0));
                    object.put("KAA01",getIntent().getIntExtra("KAA01",0));
                    object.put("KYA09L",txt_ly_time.getText().toString());
                    object.put("KMA15",ssp_ly_l);
                    object.put("KMA16",ssp_yl_r);
                    object.put("KMA17",ssp_jz_l);
                    object.put("KMA18",ssp_jzy);
                    object.put("KMA06",edit_mxb.getText().toString());
                    object.put("AKB01",bz_id);
                    if ("update".equals(getIntent().getStringExtra("tag"))){
                        object.put("addoredit","edit");
                        object.put("KMA01",manyiduEntity.getMyd_id());
                        Log.d("aaaaa",manyiduEntity.getMyd_id()+"");
                    }else {
                        object.put("addoredit","add");
                    }
                    Log.d("ccc",bz_id+"");
                    object.put("KMA08",edit_yy.getText().toString());
                    object.put("KMA09",pj_id);
                    object.put("KMA10",txt_time.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = object.toString();
                result = NetWorkUtils.getCount(baseClass.add_myd_URL,json);
                Log.d("ccc",result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject object1 = new JSONObject(result);
                            boolean b = object1.getBoolean("ifsuccess");
                            if (b){
                                finish();
                            }else {
                                Toast.makeText(AddMYDActivity.this, "添加满意度失败", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }).start();
    }
}
